import Vue from 'vue'
import VueRouter from 'vue-router'

import Login from '../views/login/Login.vue'
import Layout from '../views/layout/Layout.vue'



Vue.use(VueRouter)

const routes = [
  //登陆组件
  {
    path:"/login",
    component:Login
  },
  //后台首页
  {
    path:"/",
    component:Layout,
    redirect:"/home",
    meta:{path:'/home',title:"后台首页"},
    children:[
      {
        path:'/home',
        component:()=>import('../views/home/Home.vue'),
      }
    ]
  },

  //账号管理  account
  {
    path:"/acc",
    component:Layout,
    redirect:'/acc/acc-list',
    meta:{path:'/acc',title:"账号管理"},

    children:[
      {
        path:"/acc/acc-list",
        component:()=>import ('../views/account/AccList.vue'),
        meta:{path:'/acc/acc-list',title:"账号列表"},

      },
      {
        path:"/acc/add-acc",
        component:()=>import ('../views/account/AddAcc.vue'),
        meta:{path:'/acc/add-acc',title:"添加账号"},
      
      },
      {
        path:"/acc/password",
        component:()=>import ('../views/account/Password.vue'),
        meta:{path:'/acc/password',title:"修改密码"},
      },
      {
        path:"/acc/personal",
        component:()=>import ('../views/account/Personal.vue'),
        meta:{path:'/acc/personal',title:"个人中心"},
      },
    ]
    
  },

  //商品管理
  {
    path:"/goods",
    component:Layout,
    redirect:'/goods/goods-list',
    meta:{path:'/goods',title:"商品管理"},

    children:[
      {
        path:'/goods/goods-list',
        component:()=>import ('../views/goods/GoodsList.vue'),
        meta:{path:'/goods/goods-list',title:"商品列表"},
      },
      {
        path:'/goods/add-goods',
        component:()=>import ('../views/goods/AddGoods.vue'),
        meta:{path:'/goods/add-goods',title:"添加商品"},
      },
      {
        path:'/goods/goods-cate',
        component:()=>import ('../views/goods/GoodsCate.vue'),
        meta:{path:'/goods/goods-cate',title:"商品分类"},
      },
    ]

  },

  //订单管理
  {
    path:'/order',
    component:Layout,
    meta:{path:'/order',title:"订单管理"},
   
    children:[
      {
        path:'/',
        component:()=>import ('../views/order/Order.vue')
      }
    ]
  },

  //销售统计
  {
    path:'/sell',
    component:Layout,
    redirect:"/sell/goods-total",
    meta:{path:'/sell',title:"销售管理"},
    children:[
      {
        path:'/sell/goods-total',
        component:()=>import ('../views/sell/GoodsTotal.vue'),
        meta:{path:'/sell/goods-total',title:"商品统计"},
      },
      {
        path:'/sell/order-total',
        component:()=>import ('../views/sell/OrderTotal.vue'),
        meta:{path:'/sell/order-total',title:"订单统计"},
      }
      

    ]
  },

  //店铺管理
  {
    path:'/shop',
    component:Layout,
    meta:{path:'/shop',title:"店铺管理"},
    children:[
      {
        path:'/',
        component:()=>import ('../views/shop/Shop.vue')
      }
    ]
  },

]

const router = new VueRouter({
  routes
})


// 在暴露前添加全局卫士

router.beforeEach((to,from,next)=>{

  //from 你从哪里来?
  //to 你到哪里去?
  //next 同意,放行
  //判断本地存储是否token，如果有就直接放行，没有就返回到登陆界面

let isLogin=localStorage.getItem("t_k")? true:false

if (isLogin) {  //放行
  next();
}else{
  if (to.path=="/login") {  
    next()
  }else{
    next("/login")
  }
}

})


export default router
